/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2008 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.IO;
using System.Threading;

//using GLib;
using Gtk;

using Galaxium.Gui;
using Galaxium.Gui.GtkGui;
using Galaxium.Client;
using Galaxium.Protocol;
using Galaxium.Core;

using Anculus.Core;
using Anculus.Gui;

namespace Galaxium.Client.GtkGui
{
	public static class GtkActivityPreProcessor
	{
		private static IConfigurationSection Config = Configuration.Activity.Section;
		private static int MaxMessageLength = 35;
		
		static void OnEntityPresenceChangeActivity (object sender, EntityPresenceChangeActivity activity)
		{
			if (activity.Entity is IContact)
			{
				if (Config.GetBool (Configuration.Activity.PresenceChanges.Name, Configuration.Activity.PresenceChanges.Default))
				{
					IContact contact = (IContact)activity.Entity;
					
					GtkNotification notification = null;
					string message = string.Format("{0} is now {1}",
					                               GLib.Markup.EscapeText (MessageUtility.StripMarkup (contact.DisplayIdentifier, contact.Session.Account.Protocol)),
					                               contact.Presence.State);
					
					if (contact.DisplayImage == null || contact.DisplayImage.ImageBuffer == null || contact.DisplayImage.ImageBuffer.Length == 0)
						notification = new GtkNotification ("Status Changed", message, IconUtility.GetIcon (contact.Presence.Icon, IconSizes.Large).SaveToBuffer ("png"));
					else
						notification = new GtkNotification ("Status Changed", message, PixbufUtility.GetFramedPixbuf (new Gdk.Pixbuf(contact.DisplayImage.ImageBuffer), PixbufRendererFrameSize.Large).SaveToBuffer ("png"));
					
					NotificationUtility.Show (notification, 3000);
				}
			}
			else if (activity.Entity is IAccount)
			{
				NotificationUtility.UpdateStatus(activity.Entity);
			}
		}
		
		static void OnEntityNameChangeActivity (object sender, EntityNameChangeActivity activity)
		{
			if (Config.GetBool (Configuration.Activity.NameChanges.Name, Configuration.Activity.NameChanges.Default))
			{
				IContact contact = (IContact)activity.Entity;
				
				GtkNotification notification = null;
				string message = string.Format("{0} is now known as:\n{1}",
				                               GLib.Markup.EscapeText (MessageUtility.StripMarkup (activity.Old, contact.Session.Account.Protocol)),
				                               GLib.Markup.EscapeText (MessageUtility.StripMarkup (contact.DisplayName, contact.Session.Account.Protocol)));
				
				if (contact.DisplayImage == null || contact.DisplayImage.ImageBuffer == null || contact.DisplayImage.ImageBuffer.Length == 0)
					notification = new GtkNotification ("Name Changed", message, IconUtility.GetIcon (contact.Presence.Icon, IconSizes.Large).SaveToBuffer("png"));
				else
					notification = new GtkNotification ("Name Changed", message, PixbufUtility.GetFramedPixbuf(new Gdk.Pixbuf(contact.DisplayImage.ImageBuffer), PixbufRendererFrameSize.Large).SaveToBuffer("png"));
				
				NotificationUtility.Show(notification, 3000);
			}
		}
		
		static void OnEntityMessageChangeActivity (object sender, EntityMessageChangeActivity activity)
		{
			if (Config.GetBool (Configuration.Activity.MessageChanges.Name, Configuration.Activity.MessageChanges.Default))
			{
				IContact contact = (IContact)activity.Entity;
				
				GtkNotification notification = null;
				string message = string.Format ("{0} has changed their personal message to:\n{1}",
				                                GLib.Markup.EscapeText (MessageUtility.StripMarkup (contact.DisplayIdentifier, contact.Session.Account.Protocol)),
				                                GLib.Markup.EscapeText (MessageUtility.StripMarkup (contact.DisplayMessage, contact.Session.Account.Protocol)));
				
				if (contact.DisplayImage == null || contact.DisplayImage.ImageBuffer == null || contact.DisplayImage.ImageBuffer.Length == 0)
					notification = new GtkNotification ("Message Changed", message, IconUtility.GetIcon (contact.Presence.Icon, IconSizes.Large).SaveToBuffer("png"));
				else
					notification = new GtkNotification ("Message Changed", message, PixbufUtility.GetFramedPixbuf(new Gdk.Pixbuf(contact.DisplayImage.ImageBuffer), PixbufRendererFrameSize.Large).SaveToBuffer("png"));
				
				NotificationUtility.Show(notification, 3000);
			}
		}
		
		static void OnEntityImageChangeActivity (object sender, EntityImageChangeActivity activity)
		{
			if (activity.Entity.DisplayImage.ImageBuffer.Length < 1)
				return;
			
			if (Config.GetBool (Configuration.Activity.ImageChanges.Name, Configuration.Activity.ImageChanges.Default))
			{
				IContact contact = (IContact)activity.Entity;
				
				GtkNotification notification = null;
				string message = string.Format ("{0} has changed their display image.",
				                                GLib.Markup.EscapeText (MessageUtility.StripMarkup (activity.Entity.DisplayIdentifier, activity.Entity.Session.Account.Protocol)));
				
				if (contact.DisplayImage == null || contact.DisplayImage.ImageBuffer == null || contact.DisplayImage.ImageBuffer.Length == 0)
					notification = new GtkNotification ("Image Changed", message, IconUtility.GetIcon (contact.Presence.Icon, IconSizes.Large).SaveToBuffer("png"));
				else
					notification = new GtkNotification ("Image Changed", message, PixbufUtility.GetFramedPixbuf(new Gdk.Pixbuf(contact.DisplayImage.ImageBuffer), PixbufRendererFrameSize.Large).SaveToBuffer("png"));
				
				NotificationUtility.Show (notification, 3000);
			}
		}
		
		static void OnReceivedMessageActivity (object sender, ReceivedMessageActivity activity)
		{
			int done = 0;
			
			if (WindowUtility<Widget>.GetWindow (activity.Conversation) != null)
			{
				return;
			}
			else
			{
				//Log.Debug ("There is no conversation window.");
			}
			
			if (Config.GetBool (Configuration.Activity.NewConversations.Name, Configuration.Activity.NewConversations.Default))
			{
				IContact contact = (IContact)activity.Entity;
				
				string text = activity.Message.GetText ();
				
				if (text.Length > MaxMessageLength)
					text = text.Substring (0, MaxMessageLength) + "...";
				
				GtkNotification notification = null;
				string message = string.Format ("{0} has sent you a message:\n{1}",
				                                GLib.Markup.EscapeText (MessageUtility.StripMarkup (contact.DisplayIdentifier, contact.Session.Account.Protocol)),
				                                text);
				
				if (contact.DisplayImage == null || contact.DisplayImage.ImageBuffer == null || contact.DisplayImage.ImageBuffer.Length == 0)
					notification = new GtkNotification ("Incoming Message", message, IconUtility.GetIcon (contact.Presence.Icon, IconSizes.Large).SaveToBuffer("png"));
				else
					notification = new GtkNotification ("Incoming Message", message, PixbufUtility.GetFramedPixbuf (new Gdk.Pixbuf (contact.DisplayImage.ImageBuffer), PixbufRendererFrameSize.Large).SaveToBuffer ("png"));
				
				NotificationUtility.Show (notification, 3000);
			}
		}
		
		static void OnReceivedOtherActivity (object sender, ReceivedOtherActivity activity)
		{
			if (Config.GetBool (Configuration.Activity.ProtocolSpecific.Name, Configuration.Activity.ProtocolSpecific.Default))
			{
				IContact contact = (IContact)activity.Entity;
				
				GtkNotification notification = null;
				string message = string.Format("{0} is trying to get your attention!",
				                               GLib.Markup.EscapeText (MessageUtility.StripMarkup (contact.DisplayIdentifier, contact.Session.Account.Protocol)));
				
				if (contact.DisplayImage == null || contact.DisplayImage.ImageBuffer == null || contact.DisplayImage.ImageBuffer.Length == 0)
					notification = new GtkNotification ("Incoming Activity", message, IconUtility.GetIcon (contact.Presence.Icon, IconSizes.Large).SaveToBuffer("png"));
				else
					notification = new GtkNotification("Incoming Activity", message, PixbufUtility.GetFramedPixbuf(new Gdk.Pixbuf(contact.DisplayImage.ImageBuffer), PixbufRendererFrameSize.Large).SaveToBuffer("png"));
				
				NotificationUtility.Show(notification, 3000);
			}
		}
		
		static void OnReceivedFileActivity (object sender, ReceivedFileActivity activity)
		{
			if (Config.GetBool (Configuration.Activity.Transfers.Name, Configuration.Activity.Transfers.Default))
			{
				IContact contact = (IContact)activity.Entity;
				
				GtkNotification notification = null;
				string message = string.Format ("{0} is trying to send you a file!",
				                                GLib.Markup.EscapeText (MessageUtility.StripMarkup (contact.DisplayIdentifier, contact.Session.Account.Protocol)));
				
				if (contact.DisplayImage == null || contact.DisplayImage.ImageBuffer == null || contact.DisplayImage.ImageBuffer.Length == 0)
					notification = new GtkNotification ("Incoming Transfer", message, IconUtility.GetIcon (contact.Presence.Icon, IconSizes.Large).SaveToBuffer("png"));
				else
					notification = new GtkNotification ("Incoming Transfer", message, PixbufUtility.GetFramedPixbuf(new Gdk.Pixbuf(contact.DisplayImage.ImageBuffer), PixbufRendererFrameSize.Large).SaveToBuffer("png"));
				
				NotificationUtility.Show (notification, 3000);
			}
		}
		
		static void OnNewContactActivity (object sender, NewContactActivity activity)
		{
			if (Config.GetBool (Configuration.Activity.NewContacts.Name, Configuration.Activity.NewContacts.Default))
			{
				string message = string.Format("A new contact has added you to their list!\n{0}", GLib.Markup.EscapeText(activity.Contact == null ? activity.UniqueIdentifier : activity.Contact.UniqueIdentifier));
				GtkNotification notification = null;
				
				notification = new GtkNotification ("New Contact", message, IconUtility.GetIcon ("galaxium-add-contact", IconSizes.Large).SaveToBuffer("png"));
				
				NotificationUtility.Show(notification, 3000);
			}
		}
	}
}